/**
 * @author Fang Lu, fanglupku@gmail.com
 * 2011-5-18
 */
package com.autotag.evaluate;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;

import fanglu.util.FileDealer;

import sei.pku.wsdl_analyzer.Endpoint;
import sei.pku.wsdl_analyzer.WSDLFile;
import sei.pku.wsdl_analyzer.WSDLFileAnalyzer;

public class ServiceSelector {
	public static void RandomSelectWSDLFile(String srcDir, 
			String destDir, int count){
		File srcDirFile = new File(srcDir);
		File destDirFile = new File(destDir);
		if (!srcDirFile.isDirectory() || !destDirFile.isDirectory()){
			return;
		}
		
		File [] files = srcDirFile.listFiles();
		HashSet<String> locationSet = new HashSet<String>();
		HashSet<Integer> indexSet = new HashSet<Integer>();
		Random random = new Random(System.currentTimeMillis());
		int sum = 0;
		WSDLFileAnalyzer wsdlFileAnalyzer = new WSDLFileAnalyzer();
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter(new File(destDir + "summary.txt")));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		while (sum < count){
			int thisIndex = Math.abs(random.nextInt()) % files.length;
			if (indexSet.add(thisIndex)){
				try {
					WSDLFile wFile = wsdlFileAnalyzer.analyzeWSDL(files[thisIndex].getAbsolutePath());
					ArrayList<Endpoint> endPointList = wFile.endPoints;
					if (endPointList.size() > 0){
						String location = endPointList.get(0).getLocation();
						if (locationSet.add(location) && location.startsWith("http")
								&& location.indexOf("localhost") <0 
								&& location.indexOf("192") < 0
								&& location.indexOf("127") < 0 ){
							System.out.println(endPointList.get(0).getLocation());
							FileDealer.copyFile(files[thisIndex], new File(destDir + files[thisIndex].getName()));
							bw.write("" + (sum+1));
							bw.write(",");
							bw.write(files[thisIndex].getName());
							bw.write(",");
							bw.write(location);
							bw.write("\n");
							bw.write("similar service uuid:");
							bw.write("\n");
							bw.write("domain tag:");
							bw.write("\n");
							bw.write("functionality tag:");
							bw.write("\n");
							++ sum;
						}
					}
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		try {
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static void RenameForDir(String fileDir){
		File srcDirFile = new File(fileDir);
		File [] files = srcDirFile.listFiles();
		for (int i=0; i<files.length; ++i){
			if (files[i].getName().endsWith("html")){
				File thisNewFile = new File(files[i].getAbsolutePath().replaceAll("html", "xml"));
				files[i].renameTo(thisNewFile);
			}
		}
	}
	public static void main(String args[]){
//		RandomSelectWSDLFile("C:\\Users\\ftc\\Desktop\\ICSOC\\WSDL_FILE\\WSDL_FILE\\", 
//				"C:\\Users\\ftc\\Desktop\\ICSOC\\WSDL_FILE\\test_set\\", 300);
		RenameForDir("C:\\Users\\ftc\\Desktop\\ICSOC\\WSDL_FILE\\test_set\\WSDL_FILE\\");
	}
}
